<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Send a query and fetch all results (if any)</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.dbx-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dbx_query</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5 &lt; 5.1.0, PECL dbx &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">dbx_query</span> &mdash; <span class="dc-title">Send a query and fetch all results (if any)</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.dbx-query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dbx_query</strong></span>
    ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$link_identifier</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$sql_statement</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
  ] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Sends a query and fetch all results.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.dbx-query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">link_identifier</code></dt>

     <dd>

      <p class="para">
       The DBX link object returned by <span class="function"><a href="dbx_connect.html" class="function">dbx_connect()</a></span>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sql_statement</code></dt>

     <dd>

      <p class="para">
       SQL statement.
      </p>
      <p class="para">
       Data inside the query should be <a href="dbx_escape_string.html" class="link">properly escaped</a>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       The <code class="parameter">flags</code> parameter is used to control the amount of
       information that is returned. It may be any combination of the following 
       constants with the bitwise OR operator (|). The DBX_COLNAMES_* flags 
       override the dbx.colnames_case setting from <var class="filename">php.ini</var>.
       <dl>

        
         <dt>

          <strong><code>DBX_RESULT_INDEX</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           It is <em class="emphasis">always</em> set, that is, the returned object 
           has a <span class="property">data</span> property which is a 2 dimensional
           array indexed numerically. For example, in the expression 
           <em>data[2][3]</em> <em>2</em> stands for the row
           (or record) number and <em>3</em> stands for the column 
           (or field) number. The first row and column are indexed at 0.
          </span>
          <span class="simpara">
           If <strong><code>DBX_RESULT_ASSOC</code></strong> is also specified, the 
           returning object contains the information related to 
           <strong><code>DBX_RESULT_INFO</code></strong> too, even if it was not specified. 
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_RESULT_INFO</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           It provides info about columns, such as field names and field types.
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_RESULT_ASSOC</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           It effects that the field values can be accessed with the respective 
           column names used as keys to the returned object&#039;s 
           <span class="property">data</span> property.
          </span>
          <span class="simpara">
           Associated results are actually references to the numerically indexed 
           data, so modifying <em>data[0][0]</em> causes that
           <em>data[0][&#039;field_name_for_first_column&#039;]</em> is modified
           as well.
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_RESULT_UNBUFFERED</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           This flag will not create the <span class="property">data</span> property, and 
           the <span class="property">rows</span> property will initially be 0. Use this 
           flag for large datasets, and use <span class="function"><a href="dbx_fetch_row.html" class="function">dbx_fetch_row()</a></span> to
           retrieve the results row by row.
          </span>
          <span class="simpara">
           The <span class="function"><a href="dbx_fetch_row.html" class="function">dbx_fetch_row()</a></span> function will return rows that
           are conformant to the flags set with this query. Incidentally, it will
           also update the <span class="property">rows</span> each time it is called.
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_COLNAMES_UNCHANGED</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           The case of the returned column names will not be changed.
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_COLNAMES_UPPERCASE</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           The case of the returned column names will be changed to 
           uppercase.
          </span>
         </dd>

        
        
         <dt>

          <strong><code>DBX_COLNAMES_LOWERCASE</code></strong>
         </dt>

         <dd>

          <span class="simpara">
           The case of the returned column names will be changed to 
           lowercase.
          </span>
         </dd>

        
       </dl>

       Note that <strong><code>DBX_RESULT_INDEX</code></strong> is always used, regardless 
       of the actual value of <code class="parameter">flags</code> parameter. This means 
       that only the following combinations are effective:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_RESULT_INDEX</code></strong>
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_RESULT_INDEX</code></strong> |
          <strong><code>DBX_RESULT_INFO</code></strong>
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_RESULT_INDEX</code></strong> |
          <strong><code>DBX_RESULT_INFO</code></strong> |
          <strong><code>DBX_RESULT_ASSOC</code></strong> - this is the default, if 
          <code class="parameter">flags</code> is not specified.
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.dbx-query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>dbx_query()</strong></span> returns an object or <em>1</em> 
   on success, and <em>0</em> on failure. The result object is 
   returned only if the query given in <code class="parameter">sql_statement</code>
   produces a result set (i.e. a SELECT query, even if the result set is
   empty). 
  </p>
  <p class="para">
   The returned object has four or five
   properties depending on <code class="parameter">flags</code>:
   <dl>

    
     <dt>

      <span class="property">handle</span>
     </dt>

     <dd>

      <p class="para">
       It is a valid handle for the connected database, and as such it can be
       used in module specific functions (if required).
       <div class="informalexample">
        <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id&nbsp;FROM&nbsp;table"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_field_len</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
        </div>

       </div>
      </p>
     </dd>

    
    
     <dt>

      <span class="property">cols</span> and <span class="property">rows</span>
     </dt>

     <dd>

      <p class="para">
       These contain the number of columns (or fields) and rows (or records)
       respectively.
       <div class="informalexample">
        <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;id&nbsp;FROM&nbsp;table'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rows</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;number&nbsp;of&nbsp;records<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cols</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;number&nbsp;of&nbsp;fields&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
        </div>

       </div>
      </p>
     </dd>

    
    
     <dt>

      <span class="property">info</span> (optional)
     </dt>

     <dd>

      <span class="simpara">
       It is returned only if either <strong><code>DBX_RESULT_INFO</code></strong> or
       <strong><code>DBX_RESULT_ASSOC</code></strong> is specified in the
       <code class="parameter">flags</code> parameter. It is a 2 dimensional array,
       that has two named rows (<em>name</em> and 
       <em>type</em>) to retrieve column information.
      </span>
      <div class="example" id="example-975">
       <p><strong>Example #1 lists each field&#039;s name and type</strong></p>
       <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;id&nbsp;FROM&nbsp;table'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">DBX_RESULT_INDEX&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">DBX_RESULT_INFO</span><span style="color: #007700">);<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cols</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++&nbsp;)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">info</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">][</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">info</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">][</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;&nbsp;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
       </div>

      </div>
     </dd>

    
    
     <dt>

      <span class="property">data</span>
     </dt>

     <dd>

      <span class="simpara">
       This property contains the actual resulting data, possibly associated 
       with column names as well depending on <code class="parameter">flags</code>.
       If <strong><code>DBX_RESULT_ASSOC</code></strong> is set, it is possible to use
       <em>$result-&gt;data[2][&quot;field_name&quot;]</em>.
      </span>
      <div class="example" id="sodium-crypto-kx-keypair.example.basic">
       <p><strong>Example #2 outputs the content of data property into HTML table</strong></p>
       <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;id,&nbsp;parentid,&nbsp;description&nbsp;FROM&nbsp;table'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;table&gt;\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;tr&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$field</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;td&gt;</span><span style="color: #0000BB">$field</span><span style="color: #DD0000">&lt;/td&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;/table&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
       </div>

      </div>
      <div class="example" id="example-977">
       <p><strong>Example #3 How to handle UNBUFFERED queries</strong></p>
       <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;id,&nbsp;parentid,&nbsp;description&nbsp;FROM&nbsp;table'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DBX_RESULT_UNBUFFERED</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;table&gt;\n"</span><span style="color: #007700">;<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;tr&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$field</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;td&gt;</span><span style="color: #0000BB">$field</span><span style="color: #DD0000">&lt;/td&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;/table&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
       </div>

      </div>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.dbx-query-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        Introduced <strong><code>DBX_RESULT_UNBUFFERED</code></strong>.
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        Introduced <strong><code>DBX_COLNAMES_UNCHANGED</code></strong>,
        <strong><code>DBX_COLNAMES_UPPERCASE</code></strong>, and
        <strong><code>DBX_COLNAMES_LOWERCASE</code></strong>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.dbx-query-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-978">
    <p><strong>Example #4 How to handle the returned value</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">DBX_ODBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;id,&nbsp;parentid,&nbsp;description&nbsp;FROM&nbsp;table'</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">is_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...&nbsp;do&nbsp;some&nbsp;stuff&nbsp;here,&nbsp;see&nbsp;detailed&nbsp;examples&nbsp;below&nbsp;...<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;first,&nbsp;print&nbsp;out&nbsp;field&nbsp;names&nbsp;and&nbsp;types&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;then,&nbsp;draw&nbsp;a&nbsp;table&nbsp;filled&nbsp;with&nbsp;the&nbsp;returned&nbsp;field&nbsp;values<br /></span><span style="color: #007700">}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Query&nbsp;failed"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">dbx_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.dbx-query-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Always refer to the module-specific documentation as well.
   </p>
   <p class="para">
    Column names for queries on an Oracle database are returned in lowercase.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.dbx-query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="dbx_escape_string.html" class="function" rel="rdfs-seeAlso">dbx_escape_string()</a> - Escape a string so it can safely be used in an sql-statement</span></li>
    <li class="member"><span class="function"><a href="dbx_fetch_row.html" class="function" rel="rdfs-seeAlso">dbx_fetch_row()</a> - Fetches rows from a query-result that had the 
  DBX_RESULT_UNBUFFERED flag set</span></li>
    <li class="member"><span class="function"><a href="dbx_connect.html" class="function" rel="rdfs-seeAlso">dbx_connect()</a> - Open a connection/database</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>